<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
         * 数据类型转换通常指将其他数据类型转成 字符串 数字 布尔值
         * 数据类型转换有两种: 强制转换(显式转换) 和 自动转换(隐式转换)
         * 
         * 强制转换(显式转换): 其他类型 转成 字符串
         * 
         * val.toString()将val转成字符串值(null和undefined不能转)
         * null、undefined会报错。  [] 会打印 ''
         * 
         * String(val)将val转成字符串值(null和undefined也能转)
        */
        var str1 = '';// 空串
        var str2 = 'hello';// 非空串
        var num1 = 0;// 0
        var num2 = 123;// 非0
        var num3 = NaN;
        var boo1 = true;
        var boo2 = false;
        var nul = null;
        var und = undefined;
        var obj = { a: 1, b: 2 };
        var fun = function () { };
        var arr1 = [];
        var arr2 = [6];
        var arr3 = [7, 8, 9];

        console.log(str1.toString());//''
        console.log(str2.toString());//'hello'
        console.log(num1.toString());//'0'
        console.log(num2.toString());//'123'
        console.log(num3.toString());//'NaN'
        console.log(boo1.toString());//'true'
        console.log(boo2.toString());//'false'
        //console.log(nul.toString());//报错
        //console.log(und.toString());//报错
        console.log(obj.toString());//'[object Object]'
        console.log(fun.toString());//'function(){}'
        console.log(arr1.toString());//''
        console.log(arr2.toString());//'6'
        console.log(arr3.toString());//'7,8,9'
       

        console.log(String(str1));//''
        console.log(String(str2));//'hello'
        console.log(String(num1));//'0'
        console.log(String(num2));//'123'
        console.log(String(num3));//'NaN'
        console.log(String(boo1));//'true'
        console.log(String(boo2));//'false'
        console.log(String(nul));//'null'
        console.log(String(und));//'undefined'
        console.log(String(obj));//'[object Object]'
        console.log(String(fun));//'function(){}'
        console.log(String(arr1));//''
        console.log(String(arr2));//'6'
        console.log(String(arr3));//'7,8,9'
    </script>
</body>

</html>